import { Enum, Sprite, SpriteFrame } from 'cc';
import { Prefab } from 'cc';
import { find } from 'cc';
import { UITransform } from 'cc';
import { Vec3 } from 'cc';
import { Vec2 } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { gameComponent } from '../gameComponent';
import { Canvas } from 'cc';
import { Camera } from 'cc';
import { resourceManager, resourceType } from '../resource/resourceManager';
import { UIManager } from '../UIManager';
import { buildComponent } from '../build/buildComponent';
import { instantiate } from 'cc';
const { ccclass, property } = _decorator;

enum attackType {
    MELEE,
    Archer
}

let speedRate = 0.02;
let rangeRate = 3;

@ccclass('roleComponent')
export class roleComponent extends Component {

    @property(Prefab)
    wood_prefab : Prefab = null; // 木头

    @property(Prefab)
    meat_prefab : Prefab = null; // 木头
      
    @property({type : Camera}) //移动速度
    roleCam : Camera = null;

    @property({type : Node}) //移动速度
    moveBg : Node = null;

    @property(Node)
    gameUI : Node = null; // 攻击效果

    @property({type : Number}) //移动速度
    moveSpeed = 100;

    @property({type : Enum(attackType)}) //攻击类型
    attackType : attackType = attackType.MELEE;

    @property({type : Number}) //攻击范围
    attackRange = 100;

    @property({type : Number}) //攻击范围
    getRange = 50;

    @property({type : Number}) //获取道具的范围
    toolRange = 100;

    @property({type : Number}) //攻击力
    attackPower = 100;

    @property({type : Number}) //攻击速度
    attackSpeed = 1;

    @property({type : Number}) //血量 如果血量为-1 则视为无限血量
    blood = -1;

    @property(Prefab)
    attackEffect : Prefab = null; // 攻击效果

    @property(SpriteFrame)
    roleSkinList : SpriteFrame[] = [];

    private curMoveDir = new Vec2(0,1);

    private curMoveSpeed = this.moveSpeed;

    //玩家获取的木头
    private curProccessWood = 0;

    //玩家获取的肉
    private curProccessMeat = 0;

    private curBlood = this.blood;

    isCanMove = false;

    start() {
        // this.setRoleSkin(this.attackType);
    }

    setMoveStatus(status){
        this.isCanMove = status;
    }

    setRoleSkin(type){
        let pic = this.node.getChildByName("pic");
        pic.getComponent(Sprite).spriteFrame = this.roleSkinList[type];
    }

    roleMovePosition(){
        if(!this.isCanMove) return;
        let cur = new Vec2(this.curMoveDir.x,this.curMoveDir.y);
        let pos = new Vec2(this.node.position.x,this.node.position.y);
        let scale = new Vec2(this.curMoveSpeed * speedRate,this.curMoveSpeed * speedRate);
        let result = new Vec2(0,0);
        let moveDelte =  cur.multiply(scale)
        Vec2.add(result, pos,moveDelte);
        this.node.setPosition(new Vec3(result.x,result.y,0));

        this.roleMoveChangeCameraPosition(cur.multiply(scale));
    }

    roleMoveChangeCameraPosition(moveDelte){
        let camPos1 = new Vec2(this.roleCam.node.position.x,this.roleCam.node.position.y);
        let camPos2 = new Vec2(0,0);
        Vec2.add(camPos2, camPos1, moveDelte);
        this.roleCam.node.setPosition(new Vec3(camPos2.x,camPos2.y,0));

        let viewPort = this.roleCam.fov;
        console.log(viewPort);
    }

    changeRoleDir(dir){
        this.curMoveDir = dir;
        let pic = this.node.getChildByName("pic");
        if(dir.x > 0){
            pic.scale = new Vec3(1,1,1);
            pic.position = new Vec3(8.6,0,0);
        }else{
            pic.scale = new Vec3(-1,1,1);
            pic.position = new Vec3(-15,0,0);
        }
    }

    attackEnemy(enemy){
        if(!this.isCanAttack) return;
        console.log("========英雄可以攻击敌人=======");
        this.attackTimes = 0;
        find("Canvas").getComponent(gameComponent).createHeroAttackEnemyEffect(this.node,enemy);
    }

    getResources(resource,type){
        resource.getComponent(resourceManager).moveToHero(this.node,()=>{
            if(type == resourceType.WOOD){
                this.curProccessWood += 1;
                this.gameUI.getComponent(UIManager).updateWoodNumber(this.curProccessWood);
                console.log("英雄拥有的木头数量:",this.curProccessWood);
            }else if(type == resourceType.MEAT){
                this.curProccessMeat += 1;
                this.gameUI.getComponent(UIManager).updateMeatNumber(this.curProccessMeat);
                console.log("英雄拥有的肉数量:",this.curProccessWood);
            }
        })
    }

    giveResources(build){
        let tool_number = build.getComponent(buildComponent).judgeNeedTool();
        if(tool_number != 0){
            for(let i = 0 ; i < tool_number ; i ++){
           
            }
        }
    }

    checkCloseToTool(range){
        return range <= this.getRange ? true : false;
    }

    checkAttackEnemyRange(range){
        return range <= this.attackRange ? true : false;
    }

    checkAttackTreeRange(range){
        return range <= this.toolRange ? true : false;
    }

    attackTree(tree){
        if(!this.isCanAttack) return;
        console.log("========英雄可以攻击建筑=======");
        this.attackTimes = 0;
        find("Canvas").getComponent(gameComponent).createHeroAttackTreeEffect(this.node,tree);
    }

    attackTimes = 0;
    isCanAttack = true;
    updateAttackChiling(){
        this.attackTimes++;
        if(this.attackTimes >= 50){
            this.isCanAttack = true;
        }else{
            this.isCanAttack = false;
        }
    }

    update(deltaTime: number) {
        //人物移动
        this.roleMovePosition();

        this.updateAttackChiling();
    }
}


